<template>
  <div class="qr-container">
    <div class="input-area">
      <input v-model="text" placeholder="请输入要生成二维码的内容" />
      <button @click="generateQR">生成二维码</button>
    </div>
    
    <div class="qr-result">
      <div v-if="isLoading" class="loading-spinner">
        <div class="spinner"></div>
      </div>
      <img v-else-if="qrImageUrl" :src="qrImageUrl" class="image"  alt="生成的二维码" />
      <p v-else>请输入内容并点击生成按钮</p>
    </div>
  </div>
</template>

<script>
import QRCode from 'qrcode';

export default {
  name: 'QrCodeGenerator',
  data() {
    return {
      text: '',
      qrImageUrl: '',
      isLoading: false
    }
  },
  methods: {
    async generateQR() {
      if (!this.text) {
        alert('请输入内容');
        return;
      }
      
      this.isLoading = true;
      this.qrImageUrl = '';
      
      try {
        // 模拟生成过程需要一些时间
        await new Promise(resolve => setTimeout(resolve, 1000));
        
        // 生成二维码
        const url = await QRCode.toDataURL(this.text);
        this.qrImageUrl = url;
      } catch (error) {
        console.error('生成二维码出错:', error);
        alert('生成二维码失败，请重试');
      } finally {
        this.isLoading = false;
      }
    }
  }
}
</script>

<style scoped>
.qr-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 20px;
  padding: 20px;
}

.input-area {
  display: flex;
  gap: 10px;
  width: 100%;
  max-width: 500px;
}

.input-area input {
  flex: 1;
  padding: 8px 12px;
  border: 1px solid #ccc;
  border-radius: 4px;
}

.input-area button {
  padding: 8px 16px;
  background-color: #4CAF50;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

.input-area button:hover {
  background-color: #45a049;
}

.qr-result {
  width: 256px;
  height: 256px;
  display: flex;
  justify-content: center;
  border: 1px solid #eee;
}

.image{
  max-width: 150%;
  max-height: 150%;
  transition: transform 0.3s ease;
}

/* 旋转动画样式 */
.loading-spinner {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 100%;
}

.spinner {
  width: 50px;
  height: 50px;
  border: 5px solid rgba(0, 0, 0, 0.1);
  border-radius: 50%;
  border-top-color: #4CAF50;
  animation: spin 1s ease-in-out infinite;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}
</style>